<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Dao;

use App\Model\LoginLog;
use App\Model\User;

class UserDao
{
    /**
     * 获取用户信息.
     * @param mixed $userId
     */
    public function getUserInfo($userId): array
    {
        return User::findOrFail($userId, ['user_id', 'nickname', 'account', 'gender', 'avatar_img'])->append('gender_name')->toArray();
    }

    /**
     * 获取用户信息通过手机号.
     */
    public function getUserByAccount(string $account): array
    {
        $userInfo = User::where('account', $account)->first();
        if (is_null($userInfo)) {
            $userInfo = [];
        } else {
            $userInfo = $userInfo->toArray();
        }
        return $userInfo;
    }

    /**
     * 新增一个用户.
     * @param $account
     */
    public function addUser($account): int
    {
        /**
         * @var User $user
         */
        $user = new User();
        $user->account = $account;
        $user->nickname = $account;
        $user->save();
        return $user->user_id;
    }

    /**
     * 新增一个登录记录.
     * @param mixed $userId
     */
    public function addLoginLog($userId): void
    {
        /**
         * @var LoginLog $loginLog
         */
        $loginLog = new LoginLog();
        $loginLog->user_id = $userId;
        $loginLog->date = date('Y-m-d');
        $loginLog->save();
    }
}
